﻿#include "CardGame.h"
#include<string.h>
USING_NS_CC;

using namespace std;

CardGame::CardGame() {
	selfCards_node = Node::create();
}

//判断是否有王
bool hasKing(vector<int> arr) {
	for (int i = 0; i < arr.size(); i++) {
		if (arr[i] >= 52)
		{
			return true;//有
		}
		return false;
	}
}


//判断对子
bool CardGame::isKingBomb(vector<int> arr) {
	if ((arr[0] == 52 && arr[1] == 53) || (arr[0] == 53 && arr[1] == 52))
	{
		return true;

	}
	else
	{
		return false;
	}
}
bool CardGame::isRedTenBomb(vector<int> arr) {
	if ((arr[0] == 22 && arr[1] == 48) || (arr[0] == 48 && arr[1] == 22))
	{
		return true;

	}
	else
	{
		return false;
	}
}
bool CardGame::isDouble(vector<int> arr) {
	//先排除大小王，排除十
	if (hasKing(arr))
	{
		return false;
	}
	else if (arr[0] % 13 == arr[1] % 13)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//判断是否为炸弹(3-4张牌)
bool isThreeOrFourBomb(vector<int> arr) {
	//先排除包含王
	if (hasKing(arr)) {
		return false;
	}
	bool allSame = true;//判断是否全部相同
	for (int i = 0; i < arr.size(); i++) {
		if (arr[0] % 13 != arr[1] % 13) {//不相同就false
			allSame = false;
			break;
		}
	}
	if (allSame) {
		return true;
	}
	else {
		return false;
	}
}
//判断顺子（至少3连）123/234/....QKA
bool isStraight(vector<int> arr) {
	if (hasKing(arr)) {
		return false;
	}
	for (int i = 0; i < arr.size(); i++)
	{
		if (arr[i]%13==arr[i+1]%13+1) {
			return true;
		}
		/*else if (arr[i] % 13== arr[i + 1] % 13+12) {
			return true;
		}*/
		else {
			return false;
		}
	}
	//A 2 3 4 5 6 7 8 9 10 J Q K A
	//vector<int>vec(14, 0);//14个元素，初始值为0
	//for (int i = 0; i < vec.size(); i++) {
	//	vec[arr[i] % 13] += 1;
	//}
	////如何判断是否有K并且也有A?
	//if (vec[12]>0&&vec[0]>0) {
	//	vec[0]--;
	//	vec[13]++;
	//}
	//int count = 0;

	//for (int i = 0; i < vec.size(); i++) {
	//	if (vec[i]==1) {
	//		count++;
	//	}
	//	else if (count!=0) {
	//		break;
	//	}
	//}
	//if (count==arr.size()) {
	//	return true;
	//}
}

//通过传入所选的牌，获取牌型
PokerType CardGame::getPokerTypeWithCards(vector<int> arr) {
	int len = (int)arr.size();
	//for (int i = 0; i < arr.size(); i++)
	//{

	//}
	if (len == 1)
	{
		return PokerType::POKER_TYPE_SINGLE;
	}
	else if (len == 2)
	{
		if (isKingBomb(arr))
		{
			return PokerType::POKER_TYPE_KING_BOMB;
		}
		else if (isRedTenBomb(arr))
		{
			return PokerType::POKER_TYPE_REDTEN_BOMB;
		}
		else if (isDouble(arr))
		{
			return PokerType::POKER_TYPE_DOUBLE;
		}
	}
	else if (len >= 3) 
	{
		if (isThreeOrFourBomb(arr)){
			if (len==3) {
				return PokerType::POKER_TYPE_THREE_BOMB;
			}
			
			if (len==4) {
				return PokerType::POKER_TYPE_FOUR_BOMB;
			}
		}
		else if (isStraight(arr)) {
			return PokerType::POKER_TYPE_STRAIGHT;
		}
	}

	return PokerType::POKER_TYPE_ERROR;
}



void CardGame::shuffle(int a[], int len) {
	int idx, tmp, i;
	for (int i = 0; i < 54; i++) {
		a[i] = i;
	}
	srand((unsigned)time(NULL));
	for (i = len - 1; i > 0; i--)
	{
		idx = rand() % i;
		tmp = a[i];
		a[i] = a[idx];
		a[idx] = tmp;
	}
	for (i = 0; i < len; i++)
	{
		printf("%d", a[i]);
	}
}
Sprite* CardGame::getCardSpriteWithCardValue(int cardValue) {
	string file_name = "";

	if (cardValue == 52) {
		file_name = "xiaowang.png";
	}
	else if (cardValue == 53) {
		file_name = "dawang.png";
	}
	else {
		// 花色？
		int flower = cardValue / 13 + 1;
		// 牌面值？
		int value = cardValue % 13 + 1;
		if (flower == 1) { // 
			file_name = StringUtils::format("hei-%d.png", value);
		}
		else if (flower == 2) { // 
			file_name = StringUtils::format("fk-%d.png", value);
		}
		else if (flower == 3) { // 
			file_name = StringUtils::format("mh-%d.png", value);
		}
		else if (flower == 4) { // 
			file_name = StringUtils::format("hong-%d.png", value);
		}
	}

	Sprite *cardSprite = Sprite::createWithSpriteFrameName(file_name);
	cardSprite->setTag(cardValue);
	return cardSprite;
}